﻿Imports System.Windows.Forms
Imports System.ComponentModel
Imports FountainHead.ViewModel.Controls
Imports FountainHead.ViewModel

Namespace Controls

    Public Class LabelControl
        Inherits BaseControl
#Region "Constructors"
        Public Sub New(ByVal vm As LabelControlViewModel)
            MyBase.New(vm)            ' This call is required by the designer.
            InitializeComponent()

            ' Add any initialization after the InitializeComponent() call.

            'viewModelValue = New LabelControlViewModel
            BindControl()
            Name = "LabelControl" & counter.ToString()
            counter += 1
            snapOffsetX = lblSnapPoint.Left
            snapOffsetY = lblSnapPoint.Top

            terminal1Value = New Terminal(Me, New Point(133, 0), True)
            AddTerminal(terminal1Value)

            AddHandler ViewModel.PropertyChanged, AddressOf ViewModelPropertyChangedHandler
            AddHandler InputTerminal.PropertyChanged, AddressOf Terminal1PropertyChangedHandler
        End Sub
        Private Shared counter = 0
#End Region
        'Private viewModelValue As LabelControlViewModel
        Public Overloads ReadOnly Property ViewModel() As LabelControlViewModel
            Get
                Return MyBase.ViewModel
            End Get
        End Property

        Private terminal1Value As Terminal
        Public ReadOnly Property InputTerminal() As Terminal
            Get
                Return terminal1Value
            End Get
        End Property

#Region "Methods"
        Public Sub BindControl()
            lblText.DataBindings.Clear()
            lblText.DataBindings.Add("Text", ViewModel.InputCurrent, "Voltage")
        End Sub
#End Region

        Private Sub ViewModelPropertyChangedHandler(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
            Select Case e.PropertyName
                Case "Input"
                    terminal1Value.Current = ViewModel.InputCurrent

            End Select
            ClearTerminal()
            AddTerminal(terminal1Value)
        End Sub
        Private Sub Terminal1PropertyChangedHandler(ByVal sender As Object, ByVal e As PropertyChangedEventArgs)
            Select Case e.PropertyName
                Case "Current"
                    ViewModel.InputCurrent = InputTerminal.Current

            End Select
        End Sub

    End Class
End Namespace